<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="700px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-input v-model="form.id" type="hidden"></el-input>
      <el-form-item label="设备" prop="label">
        <el-input v-model="form.lable" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="地址位" prop="tagnumber">
        <el-input v-model="form.tagnumber" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="实时数据" prop="realdata">
        <el-input v-model="form.realdata" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="标签" prop="mearpiontdesc">
        <el-input v-model="form.mearpiontdesc"></el-input>
      </el-form-item>
      <el-form-item label="单位" prop="mearpiontunit">
        <el-select v-model="form.mearpiontunit" filterable placeholder="请选择">
          <el-option
            v-for="item in deptdata"
            :key="item.id"
            :label="`${item.name}-${item.symbol}`"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据类型" prop="vartype">
        <el-select v-model="form.vartype" placeholder="请选择">
          <el-option
            v-for="item in vartypelist"
            :key="item.id"
            :label="`${item.name}`"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="控制闭合" prop="cmd1">
        <el-input v-model="form.cmd1"></el-input>
      </el-form-item>
      <el-form-item label="控制断开" prop="cmd2">
        <el-input v-model="form.cmd2"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { savePointCfg } from '@/api/Product'
  import { getDateUnit } from '@/api/Factorymodel'
  export default {
    name: 'RoleManagementEdit',
    data() {
      return {
        deptdata: [],
        form: {
          id: '',
          mearpiontdesc: '',
          mearpiontunit: '',
          vartype: '',
          cmd1: '',
          cmd2: '',
        },
        rules: {
          mearpiontdesc: [
            { required: 1, message: '标签不能位空', trigger: 'blur' },
            {
              min: 1,
              max: 30,
              message: '长度在 1 到 30 个字符',
              trigger: 'blur',
            },
          ],
          mearpiontunit: [
            { required: 1, message: '请选择单位', trigger: 'blur' },
            {
              min: 1,
              max: 30,
              message: '长度在 1 到 30 个字符',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
        vartypelist: [
          { id: '1', name: 'DO' },
          { id: '2', name: 'DI' },
          { id: '3', name: 'AO' },
          { id: '4', name: 'AI' },
          { id: '5', name: 'Other' },
        ],
      }
    },
    created() {
      this.getDateUnit()
    },
    methods: {
      handleNodeClick() {
        this.form.dept = this.$refs['cascaderAddr'].getCheckedNodes()[0].data.id
      },
      showEdit(row) {
        console.log(row)
        if (!row) {
          //this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.listLoading = true
            const { msg } = await savePointCfg({
              discovid: this.form.id,
              mearpiontdesc: this.form.mearpiontdesc,
              mearpiontunit: this.form.mearpiontunit,
              vartype: this.form.vartype,
              cmd1: this.form.cmd1,
              cmd2: this.form.cmd2,
            })
            this.listLoading = false
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      async getDateUnit() {
        this.listLoading = true
        const { data } = await getDateUnit({})
        this.deptdata = data.list
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
